# -*-coding:Utf-8 -*
""" UI Table Class
"""
import bge, bgl, blf
from .zone import *
from .buttonlist import *
from .textbox import *
from .color import *

class Table(Box):
	
	structEx=[
		[{'width': 0.1, 'onClick':'', 'onDoubleClick':'', 'onSelect':''}],
		[['L0C0', {'width': 0.1, 'onClick':'', 'onDoubleClick':'', 'onSelect':''}],['L0C1', {'onClick':'', 'onDoubleClick':'', 'onSelect':''}]],
		[['L1C0', {'onClick':'', 'onDoubleClick':'', 'onSelect':''}],['L1C1', {'onClick':'', 'onDoubleClick':'', 'onSelect':''}]]
	]
	
	def __init__(self, parent,x, y, maxX=False, maxY=False, struct=False, row=False, column=False,): 
		Box.__init__(self, parent,x, y, maxX, maxY)
		self.row=row
		self.column=column
		self.maxX=maxX
		self.structElem=list()
		self.selectedLine=-1	
		self.hasHeader=True
		if struct:
			if len(struct)>=1 and len(struct[0])>=1 and type(struct[0][0])==dict:
				self.header(struct[0])
				self.struct(struct[1:])	
			else:	
				self.hasHeader=False
				self.struct(struct)	
		
	def header(self, schema):
		self.header=schema	
	
	def struct(self, struct):
		self.struct=struct	
		self.row=len(self.struct)
		self.column=len(self.struct[0])
		l=0
		self.decY=0
		print(self.hasHeader)
		if self.hasHeader is not False:
			self.structElem.append([])		
			decX=0
			c=0	
			for cl in self.header: #Init Header
				if 'label' in cl:
					if 'width' in cl and cl['width'] is not False:
						width=cl['width']
					elif len(self.header)>c and 'width' in self.header[c]:
						width=self.header[c]['width']
					else:
						width=self.structElem[l][c].w
					self.structElem[l].append(TextBox(self, decX, 'i'+str(self.decY), cl['label'], width))
					self.structElem[l][c]._font=ButtonList.DefaultFont
					self.structElem[l][c]._size=ButtonList.DefaultSize+4
					if len(cl)==2:
						if 'onClick' in cl and cl['onClick'] is not False:
							self.structElem[l][c]._onclic=cl['onClick']
						if 'onDoubleClick' in cl and cl['onDoubleClick'] is not False:
							self.structElem[l][c]._ondbclic=cl['onDoubleClick']							
					decX+=width
					if c!=len(self.header)-1:
						c+=1
			self.decY+=self.structElem[l][c].h
			l+=1
		
		for ln in self.struct:	
			self.structElem.append(LineTab(self, ln))			
			self.decY+=self.structElem[l].h
			l+=1
			
	def _draw(self, x,y,alpha):
		ButtonList._draw(self, x,y,alpha)
	
	def toggle_hm(self,cible):
		for ln in self.structElem:
			if cible in ln:
				self.selectedLine=self.structElem.index(ln)
				for aKey in self.hover_modification.keys():
					if hasattr(cible,aKey):
						value = getattr(cible,aKey)
						for e in ln:
							e._selected=True
						setattr(cible,aKey,self.hover_modification[aKey])
						self.hover_modification[aKey] = value	 
				
class LineTab(Box):
	def  __init__(self, parent, data):
		Box.__init__(self, parent,0, 'i'+str(parent.decY), parent.maxX, 0)		
		self.parent=parent
		self._font = ButtonList.DefaultFont
		self._size = ButtonList.DefaultSize
		self.hover_modification = ButtonList.DefaultHover.copy()
		self.column=[]
		
		decX=0
		c=0
		for cl in data:
			if 'width' in cl[1] and cl[1]['width'] is not False:
				width=cl[1]['width']
			elif self.parent.hasHeader and len(self.parent.header)>c and 'width' in self.parent.header[c]:
				width=self.parent.header[c]['width']
			else:
				width=False
			if 'background' not in cl[1]:
				self.column.append(TextBox(self, decX, 'i0', cl[0]))
				self.column[c]._font=ButtonList.DefaultFont
				self.column[c]._size=ButtonList.DefaultSize
			else:
				imgBox=Image('//{}'.format(cl[1]['background']))
				self.column.append(Box(self, decX, 'i0',100, 20, imgBox))
				self.column[c]._text=TextBox(self.column[c], 0, 'i0', cl[0])
				self.column[c]._text._font=ButtonList.DefaultFont
				self.column[c]._text._size=ButtonList.DefaultSize
			if width is False:
				width=self.column[c].w
			if len(cl)==2:
				if 'onClick' in cl[1] and cl[1]['onClick'] is not False:
					self.column[c]._onclic=cl[1]['onClick']
				if 'onDoubleClick' in cl[1] and cl[1]['onDoubleClick'] is not False:
					self.column[c]._ondbclic=cl[1]['onDoubleClick']							
			decX+=width
			if c!=len(data)-1:
				c+=1
		self.h=self.column[0].h
		if self.parent.maxX is False:
			x=0
			for cl in self.column:
				x+=cl.w
		else:
			x=self.parent.maxX
		self.w=x
		
	def _draw(self, x, y, alpha):
		if self.hover:
			for aKey in self.hover_modification.keys():
				if hasattr(self,aKey):
					value = getattr(self,aKey)
					setattr(self,aKey,self.hover_modification[aKey])
					self.hover_modification[aKey] = value	
		Box._draw(self, x, y, alpha)
		if self.hover:
			for aKey in self.hover_modification.keys():
				if hasattr(self,aKey):
					value = getattr(self,aKey)
					setattr(self,aKey,self.hover_modification[aKey])
					self.hover_modification[aKey] = value	
		
	
